package sz.szsmk.citizencard.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.ActivityModifyCardNickNameBinding;
import sz.szsmk.citizencard.dialog.SuccessDialog;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.mvp.contract.IModifyCardNickNameContact;
import sz.szsmk.citizencard.mvp.presenter.ModifyCardNickNamePresenter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.util.PubUtils;

/**
 * 修改卡昵称页面
 */
public class ModifyCardNickNameActivity extends
        IBaseActivity<ModifyCardNickNamePresenter, ActivityModifyCardNickNameBinding>
        implements IModifyCardNickNameContact.IModifyCardNickNameView, View.OnClickListener {

    private String cardNo;
    private String cardNickName;
    private SuccessDialog dialog1;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_modify_card_nick_name;
    }

    @Override
    protected void initInjector() {

        DaggerCommonActivityComponent.builder()
                .appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        cardNo = getIntent().getStringExtra("cardNo");
        cardNickName = getIntent().getStringExtra("cardNickName");
        dataBinding.titleLayout.settingTitle.setText("修改卡昵称");
        dataBinding.modifyCardIdEt.setText(cardNo);
        dataBinding.modifyCardNickEt.setText(cardNickName);
        dataBinding.modifyCardNickEt
                .setSelection(!TextUtils.isEmpty(cardNickName) ? cardNickName.length():0);
        dataBinding.titleLayout.back.setOnClickListener(this);
        dataBinding.userModifySure.setOnClickListener(this);
    }

    @Override
    protected void updateViews() {

    }

    @Override
    public void updataError() {

    }

    @Override
    public void getNewsInfoSucc() {

    }

    @Override
    public void modifyFaile(String message) {
        PubUtils.popTipOrWarn(this, message);
    }

    @Override
    public void modifySuccess() {
        dialog1 = new SuccessDialog(this, "卡昵称修改成功");

        dialog1.show();
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                dialog1.cancel();
                Intent intent = getIntent();
                intent.putExtra("cardNickName", cardNickName);
                setResult(RESULT_OK, intent);
                finish();
            }
        }.start();

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.back) {
            finish();
        } else if (id == R.id.user_modify_sure) {
            cardNo = dataBinding.modifyCardIdEt.getText().toString().trim();
            cardNickName = dataBinding.modifyCardNickEt.getText().toString().trim();
            if (TextUtils.isEmpty(cardNickName)) {
                Toast.makeText(this, "请输入卡昵称", Toast.LENGTH_SHORT).show();
                return;
            }
            mPresenter.modifyCardNickName(cardNo, cardNickName);
        }
    }

}
